{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  读取数据\n",
    "def readData(num):\n",
    "    data = pd.read_csv(\"D:/机器学习/野火/区块/区块\"+ str(num) + \".csv\")\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  批量读取dataframe\n",
    "for i in range(1, 14):\n",
    "    exec(\"data\" + str(i) + \"= readData(\" + str(i) + \")\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "      <th>acq_date</th>\n",
       "      <th>frp/MW</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-36.46985</td>\n",
       "      <td>141.06317</td>\n",
       "      <td>2019/10/2</td>\n",
       "      <td>4.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-36.46885</td>\n",
       "      <td>141.06352</td>\n",
       "      <td>2019/10/2</td>\n",
       "      <td>3.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-36.46086</td>\n",
       "      <td>141.10703</td>\n",
       "      <td>2019/10/1</td>\n",
       "      <td>4.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-36.36138</td>\n",
       "      <td>141.12328</td>\n",
       "      <td>2019/10/14</td>\n",
       "      <td>2.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-37.23269</td>\n",
       "      <td>141.12428</td>\n",
       "      <td>2019/10/22</td>\n",
       "      <td>36.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2079</th>\n",
       "      <td>-37.33434</td>\n",
       "      <td>142.49785</td>\n",
       "      <td>2019/12/23</td>\n",
       "      <td>3.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2080</th>\n",
       "      <td>-37.33282</td>\n",
       "      <td>142.49815</td>\n",
       "      <td>2019/12/22</td>\n",
       "      <td>6.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2081</th>\n",
       "      <td>-37.32859</td>\n",
       "      <td>142.49823</td>\n",
       "      <td>2019/12/22</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2082</th>\n",
       "      <td>-37.33557</td>\n",
       "      <td>142.49849</td>\n",
       "      <td>2019/12/21</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2083</th>\n",
       "      <td>-37.33487</td>\n",
       "      <td>142.49940</td>\n",
       "      <td>2019/12/22</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2084 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      latitude  longitude    acq_date  frp/MW\n",
       "0    -36.46985  141.06317   2019/10/2     4.9\n",
       "1    -36.46885  141.06352   2019/10/2     3.7\n",
       "2    -36.46086  141.10703   2019/10/1     4.2\n",
       "3    -36.36138  141.12328  2019/10/14     2.1\n",
       "4    -37.23269  141.12428  2019/10/22    36.7\n",
       "...        ...        ...         ...     ...\n",
       "2079 -37.33434  142.49785  2019/12/23     3.9\n",
       "2080 -37.33282  142.49815  2019/12/22     6.2\n",
       "2081 -37.32859  142.49823  2019/12/22     1.0\n",
       "2082 -37.33557  142.49849  2019/12/21     0.5\n",
       "2083 -37.33487  142.49940  2019/12/22     1.0\n",
       "\n",
       "[2084 rows x 4 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# #  整合相同时间段的数据\n",
    "# days_list = list(data1['acq_date'])\n",
    "# position = []\n",
    "# for index in days_list:\n",
    "#     address_index = [x for x in range(len(days_list)) if days_list[x] == index]\n",
    "#     position.append([index, address_index])\n",
    "\n",
    "# dict_address = dict(position)\n",
    "\n",
    "# #  着火点数量\n",
    "# print(len(dict_address['2019/10/2']))\n",
    "\n",
    "# #  着火点的dataframe的索引\n",
    "# print(dict_address['2019/10/2'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ave_frp(dataframe, day):\n",
    "    days_list = list(dataframe['acq_date'])\n",
    "    position = []\n",
    "    for index in days_list:\n",
    "        address_index = [x for x in range(len(days_list)) if days_list[x] == index]\n",
    "        position.append([index, address_index])\n",
    "\n",
    "    dict_address = dict(position)\n",
    "\n",
    "    #  着火点数量\n",
    "#     print(len(dict_address[day]))\n",
    "\n",
    "    #  着火点的dataframe的索引\n",
    "#     print(dict_address[day])\n",
    "    \n",
    "    #  计算frp在某一天内的平均值\n",
    "    ave_list = []\n",
    "    for ave in dict_address[day]:\n",
    "        ave_list.append(dataframe.iloc[ave,:][\"frp/MW\"])\n",
    "\n",
    "    #  返回平均值与着火点数量\n",
    "    ave_frp_num = np.mean(ave_list)\n",
    "    return ave_frp_num, len(dict_address[day])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23.116666666666667 30\n"
     ]
    }
   ],
   "source": [
    "#  验证\n",
    "num, day = ave_frp(data1, '2019/10/2')\n",
    "print(num, day)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "def createCSVFILE(filename, dataframe, area):\n",
    "    with open(filename, \"w+\") as file:\n",
    "        file.writelines(['地区,', '日期,', '着火点数量,', 'frp_avg\\n'])\n",
    "        years = ['2019', '2020']\n",
    "        mouths = ['10', '11', '12', '1']\n",
    "        for y in years:\n",
    "            for m in mouths:\n",
    "                if m == '11' and y == '2019':\n",
    "                    for d in range(1, 31):\n",
    "                        try:\n",
    "                            ave_num, num = ave_frp(dataframe, str(y) + '/' + str(m) + '/' + str(d))\n",
    "                            file.writelines([str(area) + ',', str(y)+'/'+str(m)+'/'+str(d)+', ', str(num)+', ', str(ave_num)+'\\n'])\n",
    "                            print(str(y) + '/' + str(m) + '/' + str(d))\n",
    "                            print(ave_num, num)\n",
    "                        except KeyError:\n",
    "                            file.writelines([str(area) + ',', str(y) + '/' + str(m) + '/' + str(d)+', ', 'none, ', 'none, \\n'])\n",
    "                            print(\"这天不存在着火点:\", str(y) + '/' + str(m) + '/' + str(d))\n",
    "\n",
    "                if m == '1' and y == '2020':\n",
    "                    for d in range(1, 8):\n",
    "                        try:\n",
    "                            ave_num, num = ave_frp(dataframe, str(y) + '/' + str(m) + '/' + str(d))\n",
    "                            file.writelines([str(area) + ',', str(y)+'/'+str(m)+'/'+str(d)+', ', str(num)+', ', str(ave_num)+'\\n'])\n",
    "                            print(str(y) + '/' + str(m) + '/' + str(d))\n",
    "                            print(ave_num, num)\n",
    "                        except KeyError:\n",
    "                            file.writelines([str(area) + ',', str(y) + '/' + str(m) + '/' + str(d)+', ', 'none, ', 'none, \\n'])\n",
    "                            print(\"这天不存在着火点:\", str(y) + '/' + str(m) + '/' + str(d))\n",
    "\n",
    "                elif (m == '10' or m == '12') and y == '2019':\n",
    "                    for d in range(1, 32):\n",
    "                        try:\n",
    "                            ave_num, num = ave_frp(dataframe, str(y) + '/' + str(m) + '/' + str(d))\n",
    "                            file.writelines([str(area) + ',', str(y)+'/'+str(m)+'/'+str(d)+', ', str(num)+', ', str(ave_num)+'\\n'])\n",
    "                            print(str(y) + '/' + str(m) + '/' + str(d))\n",
    "                            print(ave_num, num)\n",
    "                        except KeyError:\n",
    "                            file.writelines([str(area) + ',', str(y) + '/' + str(m) + '/' + str(d)+', ', 'none, ', 'none, \\n'])\n",
    "                            print(\"这天不存在着火点:\", str(y) + '/' + str(m) + '/' + str(d))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这天不存在着火点: 2019/10/1\n",
      "这天不存在着火点: 2019/10/2\n",
      "这天不存在着火点: 2019/10/3\n",
      "这天不存在着火点: 2019/10/4\n",
      "这天不存在着火点: 2019/10/5\n",
      "这天不存在着火点: 2019/10/6\n",
      "这天不存在着火点: 2019/10/7\n",
      "这天不存在着火点: 2019/10/8\n",
      "这天不存在着火点: 2019/10/9\n",
      "这天不存在着火点: 2019/10/10\n",
      "这天不存在着火点: 2019/10/11\n",
      "这天不存在着火点: 2019/10/12\n",
      "这天不存在着火点: 2019/10/13\n",
      "这天不存在着火点: 2019/10/14\n",
      "2019/10/15\n",
      "4.0 2\n",
      "这天不存在着火点: 2019/10/16\n",
      "这天不存在着火点: 2019/10/17\n",
      "这天不存在着火点: 2019/10/18\n",
      "这天不存在着火点: 2019/10/19\n",
      "这天不存在着火点: 2019/10/20\n",
      "这天不存在着火点: 2019/10/21\n",
      "这天不存在着火点: 2019/10/22\n",
      "这天不存在着火点: 2019/10/23\n",
      "这天不存在着火点: 2019/10/24\n",
      "这天不存在着火点: 2019/10/25\n",
      "这天不存在着火点: 2019/10/26\n",
      "2019/10/27\n",
      "0.4 2\n",
      "2019/10/28\n",
      "1.0 2\n",
      "这天不存在着火点: 2019/10/29\n",
      "这天不存在着火点: 2019/10/30\n",
      "这天不存在着火点: 2019/10/31\n",
      "这天不存在着火点: 2019/11/1\n",
      "这天不存在着火点: 2019/11/2\n",
      "这天不存在着火点: 2019/11/3\n",
      "这天不存在着火点: 2019/11/4\n",
      "这天不存在着火点: 2019/11/5\n",
      "这天不存在着火点: 2019/11/6\n",
      "这天不存在着火点: 2019/11/7\n",
      "这天不存在着火点: 2019/11/8\n",
      "这天不存在着火点: 2019/11/9\n",
      "这天不存在着火点: 2019/11/10\n",
      "这天不存在着火点: 2019/11/11\n",
      "这天不存在着火点: 2019/11/12\n",
      "这天不存在着火点: 2019/11/13\n",
      "这天不存在着火点: 2019/11/14\n",
      "这天不存在着火点: 2019/11/15\n",
      "这天不存在着火点: 2019/11/16\n",
      "这天不存在着火点: 2019/11/17\n",
      "2019/11/18\n",
      "46.125 4\n",
      "这天不存在着火点: 2019/11/19\n",
      "这天不存在着火点: 2019/11/20\n",
      "2019/11/21\n",
      "1.0999999999999999 3\n",
      "这天不存在着火点: 2019/11/22\n",
      "这天不存在着火点: 2019/11/23\n",
      "这天不存在着火点: 2019/11/24\n",
      "这天不存在着火点: 2019/11/25\n",
      "这天不存在着火点: 2019/11/26\n",
      "这天不存在着火点: 2019/11/27\n",
      "这天不存在着火点: 2019/11/28\n",
      "这天不存在着火点: 2019/11/29\n",
      "这天不存在着火点: 2019/11/30\n",
      "这天不存在着火点: 2019/12/1\n",
      "这天不存在着火点: 2019/12/2\n",
      "这天不存在着火点: 2019/12/3\n",
      "这天不存在着火点: 2019/12/4\n",
      "这天不存在着火点: 2019/12/5\n",
      "这天不存在着火点: 2019/12/6\n",
      "这天不存在着火点: 2019/12/7\n",
      "这天不存在着火点: 2019/12/8\n",
      "2019/12/9\n",
      "4.7 2\n",
      "这天不存在着火点: 2019/12/10\n",
      "这天不存在着火点: 2019/12/11\n",
      "这天不存在着火点: 2019/12/12\n",
      "这天不存在着火点: 2019/12/13\n",
      "这天不存在着火点: 2019/12/14\n",
      "这天不存在着火点: 2019/12/15\n",
      "这天不存在着火点: 2019/12/16\n",
      "这天不存在着火点: 2019/12/17\n",
      "这天不存在着火点: 2019/12/18\n",
      "2019/12/19\n",
      "2.45 4\n",
      "2019/12/20\n",
      "11.283333333333331 78\n",
      "2019/12/21\n",
      "1.6052631578947367 19\n",
      "2019/12/22\n",
      "3.187755102040816 49\n",
      "2019/12/23\n",
      "8.366666666666667 30\n",
      "2019/12/24\n",
      "4.840816326530612 49\n",
      "2019/12/25\n",
      "7.400862068965517 116\n",
      "2019/12/26\n",
      "3.7837500000000004 80\n",
      "2019/12/27\n",
      "3.96375 80\n",
      "2019/12/28\n",
      "4.414666666666667 75\n",
      "2019/12/29\n",
      "76.1038407821229 1432\n",
      "2019/12/30\n",
      "41.11000357270454 8397\n",
      "2019/12/31\n",
      "3.4799036834569828 7683\n",
      "2020/1/1\n",
      "9.375378787878788 4488\n",
      "2020/1/2\n",
      "6.692431192660551 5232\n",
      "2020/1/3\n",
      "6.450715174129353 3216\n",
      "2020/1/4\n",
      "30.19879329926178 14088\n",
      "2020/1/5\n",
      "1.277906976744186 86\n",
      "2020/1/6\n",
      "0.7363636363636364 11\n",
      "2020/1/7\n",
      "1.1928571428571426 14\n"
     ]
    }
   ],
   "source": [
    "createCSVFILE('区块8_frp.csv', data8, 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这天不存在着火点: 2019/10/1\n",
      "2019/10/2\n",
      "10.195 20\n",
      "2019/10/3\n",
      "5.816666666666666 6\n",
      "这天不存在着火点: 2019/10/4\n",
      "这天不存在着火点: 2019/10/5\n",
      "这天不存在着火点: 2019/10/6\n",
      "这天不存在着火点: 2019/10/7\n",
      "这天不存在着火点: 2019/10/8\n",
      "2019/10/9\n",
      "0.7 1\n",
      "这天不存在着火点: 2019/10/10\n",
      "2019/10/11\n",
      "0.9 1\n",
      "这天不存在着火点: 2019/10/12\n",
      "这天不存在着火点: 2019/10/13\n",
      "2019/10/14\n",
      "5.65 2\n",
      "这天不存在着火点: 2019/10/15\n",
      "这天不存在着火点: 2019/10/16\n",
      "这天不存在着火点: 2019/10/17\n",
      "这天不存在着火点: 2019/10/18\n",
      "这天不存在着火点: 2019/10/19\n",
      "这天不存在着火点: 2019/10/20\n",
      "这天不存在着火点: 2019/10/21\n",
      "2019/10/22\n",
      "3.8000000000000003 4\n",
      "这天不存在着火点: 2019/10/23\n",
      "这天不存在着火点: 2019/10/24\n",
      "这天不存在着火点: 2019/10/25\n",
      "这天不存在着火点: 2019/10/26\n",
      "这天不存在着火点: 2019/10/27\n",
      "这天不存在着火点: 2019/10/28\n",
      "这天不存在着火点: 2019/10/29\n",
      "这天不存在着火点: 2019/10/30\n",
      "2019/10/31\n",
      "3.0 2\n",
      "这天不存在着火点: 2019/11/1\n",
      "这天不存在着火点: 2019/11/2\n",
      "这天不存在着火点: 2019/11/3\n",
      "这天不存在着火点: 2019/11/4\n",
      "2019/11/5\n",
      "3.9 1\n",
      "这天不存在着火点: 2019/11/6\n",
      "这天不存在着火点: 2019/11/7\n",
      "这天不存在着火点: 2019/11/8\n",
      "这天不存在着火点: 2019/11/9\n",
      "这天不存在着火点: 2019/11/10\n",
      "2019/11/11\n",
      "0.5 1\n",
      "这天不存在着火点: 2019/11/12\n",
      "这天不存在着火点: 2019/11/13\n",
      "这天不存在着火点: 2019/11/14\n",
      "这天不存在着火点: 2019/11/15\n",
      "2019/11/16\n",
      "0.5 2\n",
      "2019/11/17\n",
      "7.9 1\n",
      "2019/11/18\n",
      "2.1 1\n",
      "这天不存在着火点: 2019/11/19\n",
      "这天不存在着火点: 2019/11/20\n",
      "这天不存在着火点: 2019/11/21\n",
      "2019/11/22\n",
      "13.48676470588235 68\n",
      "2019/11/23\n",
      "21.478947368421053 19\n",
      "2019/11/24\n",
      "14.650000000000002 84\n",
      "2019/11/25\n",
      "20.805882352941172 34\n",
      "2019/11/26\n",
      "0.7583333333333333 12\n",
      "2019/11/27\n",
      "5.609210526315789 76\n",
      "2019/11/28\n",
      "21.167741935483868 62\n",
      "2019/11/29\n",
      "12.397674418604652 43\n",
      "这天不存在着火点: 2019/11/30\n",
      "这天不存在着火点: 2019/12/1\n",
      "这天不存在着火点: 2019/12/2\n",
      "2019/12/3\n",
      "1.5714285714285712 7\n",
      "2019/12/4\n",
      "2.282608695652174 23\n",
      "2019/12/5\n",
      "1.7999999999999998 26\n",
      "2019/12/6\n",
      "3.6666666666666665 60\n",
      "2019/12/7\n",
      "7.248571428571429 35\n",
      "2019/12/8\n",
      "1.9625 8\n",
      "2019/12/9\n",
      "5.468965517241379 29\n",
      "2019/12/10\n",
      "1.8222222222222224 9\n",
      "2019/12/11\n",
      "3.225 4\n",
      "这天不存在着火点: 2019/12/12\n",
      "2019/12/13\n",
      "4.582692307692309 52\n",
      "2019/12/14\n",
      "17.82222222222222 18\n",
      "这天不存在着火点: 2019/12/15\n",
      "2019/12/16\n",
      "3.4535714285714283 56\n",
      "2019/12/17\n",
      "5.514102564102564 78\n",
      "2019/12/18\n",
      "9.085714285714287 21\n",
      "2019/12/19\n",
      "3.2416666666666667 12\n",
      "2019/12/20\n",
      "26.934335839598997 1197\n",
      "2019/12/21\n",
      "14.408966565349543 658\n",
      "2019/12/22\n",
      "7.422025912838634 849\n",
      "2019/12/23\n",
      "13.331578947368422 171\n",
      "2019/12/24\n",
      "2.7932203389830508 59\n",
      "2019/12/25\n",
      "9.004086538461538 832\n",
      "2019/12/26\n",
      "5.53216957605985 802\n",
      "2019/12/27\n",
      "9.053864168618269 854\n",
      "这天不存在着火点: 2019/12/28\n",
      "2019/12/29\n",
      "7.6192982456140355 342\n",
      "2019/12/30\n",
      "23.871880144879814 3037\n",
      "2019/12/31\n",
      "3.3727272727272726 1100\n",
      "2020/1/1\n",
      "7.498316498316498 1188\n",
      "2020/1/2\n",
      "8.559930615784907 1153\n",
      "2020/1/3\n",
      "9.223752495009979 501\n",
      "2020/1/4\n",
      "41.76654676258993 278\n",
      "这天不存在着火点: 2020/1/5\n",
      "这天不存在着火点: 2020/1/6\n",
      "这天不存在着火点: 2020/1/7\n"
     ]
    }
   ],
   "source": [
    "createCSVFILE('区块9_frp.csv', data9, 9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
